2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_i2c_ex1_masterRxMultiple.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 
34 //*****************************************************************************
35 //
55 //
56 //*****************************************************************************
57 
58 //*****************************************************************************
59 //
60 //Set the address for slave module. This is a 7-bit address sent in the
61 //following format:
62 //[A6:A5:A4:A3:A2:A1:A0:RS]
63 //
64 //A zero in the "RS" position of the first byte means that the master
65 //transmits (sends) data to the selected slave, and a one in this position
66 //means that the master receives data from the slave.
67 //
68 //*****************************************************************************
69 #define SLAVE_ADDRESS 0x48
70 //*****************************************************************************
71 //
72 //Specify Expected Receive data count.
73 //
74 //*****************************************************************************
75 #define RXCOUNT 0x05
76 
77 
78 //******************************************************************************
79 // MSP430FR59xx Demo - USCI_B0 I2C Master RX multiple bytes from MSP430 Slave
80 //
81 // Description: This demo connects two MSP430's via the I2C bus. The master
82 // reads 5 bytes from the slave. This is the MASTER CODE. The data from the slave
83 // transmitter begins at 0 and increments with each transfer.
84 // The USCI_B0 RX interrupt is used to know when new data has been received.
85 // ACLK = n/a, MCLK = SMCLK = BRCLK = DCO = 1MHz
86 //
87 // /|\ /|\
88 // MSP430FR5969 10k 10k MSP430F5969
89 // slave | | master
90 // ----------------- | | -----------------
91 // -|XIN P1.6/UCB0SDA|<-|----+->|P1.6/UCB0SDA XIN|-
92 // | | | | | 32kHz
93 // -|XOUT | | | XOUT|-
94 // | P1.7/UCB0SCL|<-+------>|P1.7/UCB0SCL |
95 // | | | P1.0|--> LED
96 //
97 //******************************************************************************
98 uint8_t RXData;
99 void main (void)
100 {
101  WDT_A_hold(WDT_A_BASE);
102 
103  //Set DCO frequency to 1MHz
104  CS_setDCOFreq(CS_DCORSEL_0,CS_DCOFSEL_0);
105  //Set ACLK = VLO with frequency divider of 1
106  CS_initClockSignal(CS_ACLK,CS_VLOCLK_SELECT,CS_CLOCK_DIVIDER_1);
107  //Set SMCLK = DCO with frequency divider of 1
108  CS_initClockSignal(CS_SMCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_1);
109  //Set MCLK = DCO with frequency divider of 1
110  CS_initClockSignal(CS_MCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_1);
111 
112  // Configure Pins for I2C
113  //Set P1.6 and P1.7 as Secondary Module Function Input.
114  /*
115 
116  * Select Port 1
117  * Set Pin 6, 7 to input Secondary Module Function, (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL).
118  */
119  GPIO_setAsPeripheralModuleFunctionInputPin(
120  GPIO_PORT_P1,
121  GPIO_PIN6 + GPIO_PIN7,
122  GPIO_SECONDARY_MODULE_FUNCTION
123  );
124 
125  //Set P1.0 as an output pin.
126  /*
127 
128  * Select Port 1
129  * Set Pin 0 as output
130  */
131  GPIO_setAsOutputPin(
132  GPIO_PORT_P1,
133  GPIO_PIN0
134  );
135 
136  /*
137  * Disable the GPIO power-on default high-impedance mode to activate
138  * previously configured port settings
139  */
140  PMM_unlockLPM5();
141 
142  EUSCI_B_I2C_initMasterParam param = {0};
143  param.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK;
144  param.i2cClk = CS_getSMCLK();
145  param.dataRate = EUSCI_B_I2C_SET_DATA_RATE_400KBPS;
146  param.byteCounterThreshold = RXCOUNT;
147  param.autoSTOPGeneration = EUSCI_B_I2C_SEND_STOP_AUTOMATICALLY_ON_BYTECOUNT_THRESHOLD;
148  EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, &param);
149 
150  //Specify slave address
151  EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE,
153  );
154 
155  //Set Master in receive mode
156  EUSCI_B_I2C_setMode(EUSCI_B0_BASE,
157  EUSCI_B_I2C_RECEIVE_MODE
158  );
159 
160  //Enable I2C Module to start operations
161  EUSCI_B_I2C_enable(EUSCI_B0_BASE);
162 
163  EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE,
164  EUSCI_B_I2C_RECEIVE_INTERRUPT0 +
165  EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT +
166  EUSCI_B_I2C_NAK_INTERRUPT
167  );
168 
169  //Enable master Receive interrupt
170  EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE,
171  EUSCI_B_I2C_RECEIVE_INTERRUPT0 +
172  EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT +
173  EUSCI_B_I2C_NAK_INTERRUPT
174  );
175 
176  while (1)
177  {
178  __delay_cycles(2000);
179 
180  while (EUSCI_B_I2C_SENDING_STOP ==
181  EUSCI_B_I2C_masterIsStopSent(EUSCI_B0_BASE));
182 
183  EUSCI_B_I2C_masterReceiveStart(EUSCI_B0_BASE);
184 
185  // Enter LPM0 w/ interrupt
186  __bis_SR_register(CPUOFF+GIE);
187  }
188 }
189 
190 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
191 #pragma vector=USCI_B0_VECTOR
192 __interrupt
193 #elif defined(__GNUC__)
194 __attribute__((interrupt(USCI_B0_VECTOR)))
195 #endif
196 void USCIB0_ISR(void)
197 {
198  static uint8_t count = 0;
199  switch(__even_in_range(UCB0IV, USCI_I2C_UCBIT9IFG))
200  {
201  case USCI_NONE: // No interrupts break;
202  break;
203  case USCI_I2C_UCALIFG: // Arbitration lost
204  break;
205  case USCI_I2C_UCNACKIFG: // NAK received (master only)
206  EUSCI_B_I2C_masterReceiveStart(EUSCI_B0_BASE);
207  break;
208  case USCI_I2C_UCSTTIFG: // START condition detected with own address (slave mode only)
209  break;
210  case USCI_I2C_UCSTPIFG: // STOP condition detected (master & slave mode)
211  break;
212  case USCI_I2C_UCRXIFG3: // RXIFG3
213  break;
214  case USCI_I2C_UCTXIFG3: // TXIFG3
215  break;
216  case USCI_I2C_UCRXIFG2: // RXIFG2
217  break;
218  case USCI_I2C_UCTXIFG2: // TXIFG2
219  break;
220  case USCI_I2C_UCRXIFG1: // RXIFG1
221  break;
222  case USCI_I2C_UCTXIFG1: // TXIFG1
223  break;
224  case USCI_I2C_UCRXIFG0: // RXIFG0
225  // Get RX data
226  RXData = EUSCI_B_I2C_masterReceiveSingle(
227  EUSCI_B0_BASE
228  );
229  if (++count >= RXCOUNT) {
230  count = 0;
231  __bic_SR_register_on_exit(CPUOFF); // Exit LPM0
232  }
233  break; // Vector 24: RXIFG0 break;
234  case USCI_I2C_UCTXIFG0: // TXIFG0
235  break;
236  case USCI_I2C_UCBCNTIFG: // Byte count limit reached (UCBxTBCNT)
237  GPIO_toggleOutputOnPin(
238  GPIO_PORT_P1,
239  GPIO_PIN0
240  );
241  break;
242  case USCI_I2C_UCCLTOIFG: // Clock low timeout - clock held low too long
243  break;
244  case USCI_I2C_UCBIT9IFG: // Generated on 9th bit of a transmit (for debugging)
245  break;
246  default:
247  break;
248  }
249 }
250 
void USCIB0_ISR(void)
MPU_initThreeSegmentsParam param
__bic_SR_register_on_exit(LPM3_bits|GIE)
__delay_cycles(500000)